package com.ryan.rss.war.ui.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ryan.rss.war.R;
import com.ryan.rss.war.database.DBManager;
import com.ryan.rss.war.util.RSSItem;
import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitlePageIndicator;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("ValidFragment")
public class ArticleFragment extends Fragment {

	private final static String LOGTAG = "ArticleFragment";

	private List<String> categoryList;
	private HashMap<String, List<RSSItem>> data;
	// main layout
	View view;

	public static List<Fragment> fragmentList;

	ArticleListFragment articleListFragment;

	FragmentPagerAdapter pagerAdapter;
	ViewPager pager;
	TabPageIndicator indicator;

	public ArticleFragment() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
	}

	private void initData() {

		DBManager dbManager = DBManager.get();
		dbManager.openDataBase();
		categoryList = dbManager.queryCategory();
		// data = dbManager.queryCategoryWithData();

		fragmentList = new ArrayList<Fragment>();

		for (int i = 0; i < categoryList.size(); i++) {

			String category = categoryList.get(i);

			List<RSSItem> rssList = dbManager.queryFromCategory(category);
			if (rssList == null) {
				Log.v(LOGTAG, "rssList is null");
			}
			ArticleListFragment fragment = new ArticleListFragment(category,
					rssList);
			fragmentList.add(fragment);

		}
		dbManager.closeDataBase();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View rootView = (View) inflater.inflate(
				R.layout.article_fragment, container, false);
		initData();
		// Initialize the adapter
		pagerAdapter = new FragmentListAdapter(getChildFragmentManager());

		// Initialize the ViewPager
		pager = (ViewPager) rootView
				.findViewById(R.id.fragment_home_phone_pager);
		// Attch the adapter
		pager.setAdapter(pagerAdapter);
		// 全部缓存
		// Offscreen pager loading limit
		pager.setOffscreenPageLimit(pagerAdapter.getCount() - 1);
		// Start on the last page the user was on

		// Initialze the TPI
		final TitlePageIndicator pageIndicator = (TitlePageIndicator) rootView
				.findViewById(R.id.fragment_home_phone_pager_titles);
		// Attach the ViewPager
		pageIndicator.setViewPager(pager);
		// Scroll to the current artist, album, or song
		// pageIndicator.setOnCenterItemClickListener(this);
		return rootView;

	}

	class FragmentListAdapter extends FragmentPagerAdapter {

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {

			super.destroyItem(container, position, object);
		}

		@Override
		public void finishUpdate(ViewGroup container) {

			super.finishUpdate(container);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {

			return super.instantiateItem(container, position);
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {

			super.restoreState(state, loader);
		}

		public FragmentListAdapter(FragmentManager fm) {
			super(fm);

		}

		@Override
		public Fragment getItem(int position) {

			return fragmentList.get(position);
		}

		public CharSequence getPageTitle(int position) {
			return categoryList.get(position);
		}

		@Override
		public int getCount() {

			return categoryList.size();
		}

	}

}
